<template>
  <div>
    <Header></Header>

    <div class="m-content">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="摘要" prop="description">
          <el-input type="textarea" v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <mavon-editor v-model="ruleForm.content"></mavon-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">发表</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button type="danger" @click="del()" v-if="delEnable">删除</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Header from "@/components/Header";
export default {
  name: "BlogEdit",
  components: {
    Header
  },
  data() {
    return {
      ruleForm: {
        id: '',
        title: '',
        description: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入摘要', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      },
      delEnable: false
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("/blog/edit", this.ruleForm/*传递请求参数到请求体中*/,{//配置请求头
            headers: {
              "Authorization": localStorage.getItem("token"),
            }
          }).then(res=>{
            //console.log(res);
            this.$alert('操作成功', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$router.push("/blogs")
              }
            });
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    del() {
      if (this.ruleForm.id != '') {
        //console.log("可以删除")
        this.$confirm('此操作将永久删除这条博客, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post("/blog/del/" + this.ruleForm.id,{}, {
            headers: {
              "Authorization": localStorage.getItem("token"),
            }
          }).then(res=>{
            const msg = res.data.data;
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: msg
              });
              this.$router.push("/blogs");
            } else if (res.data.code == 400){
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
          }).catch(err=>{
            console.log(err);
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      } else {

      }
    }
  },
  created() {
    // 取参数
    const blogId = this.$route.params.blogId;
    // console.log(blogId)
    if (blogId) {
      this.$axios.get("/blog/" + blogId).then(res=>{
        const blog = res.data.data;
        this.ruleForm.id = blog.id;
        this.ruleForm.title = blog.title;
        this.ruleForm.description = blog.description;
        this.ruleForm.content = blog.content;
        this.delEnable = true
      })
    }
  }
}
</script>

<style scoped>
  .m-content{
    text-align: center;
    margin: 0 auto;
  }
</style>